package com.mall.sso.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.mall.pojo.TaotaoResult;
import com.mall.sso.service.LoginService;
import com.mall.utils.CookieUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Administrator
 */

@Controller
public class LoginController {

    @Reference(timeout = 10000000)
    LoginService loginService;

    @Value("${token_key}")
    private String token_key;


    @PostMapping("/user/login")
    @ResponseBody
    public TaotaoResult login(HttpServletRequest req, HttpServletResponse res,
            String username, String password){

        TaotaoResult taotaoResult = loginService.login(username, password);
        //将token写入cookie中
        CookieUtils.setCookie(req,res,token_key, (String) taotaoResult.getData());
        return taotaoResult;
    }


    @GetMapping("/user/logout")
    public String logout(String token){
        if (!StringUtils.isEmpty(token)) {
            loginService.logout(token);
        }
        return "login";
    }


}
